package com.hotel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotel.annotation.ControllerEndpoint;
import com.hotel.entity.Log;
import com.hotel.response.Result;
import com.hotel.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "操作日志管理")
@RestController
@RequestMapping("/log")
public class LogController {

    @Resource
    private LogService logService;

    @ApiOperation(value = "获取操作日志")
    @GetMapping("getOperationLog")
    public Result getOperationLog(@RequestParam(value = "username",required = false) String username,
                                  @RequestParam(value = "createTime",required = false)String createTime,
                                  @RequestParam(value = "endTime",required = false)String endTime,
                                  @RequestParam(value = "current",required = false,defaultValue = "1")Integer current,
                                  @RequestParam(value = "size",required = false,defaultValue = "4")Integer size){
        IPage<Log> log = logService.getOperationLog(username, createTime, endTime, current, size);
        return Result.ok().data("log",log);
    }

    @ApiOperation(value = "根据id获取操作日志")
    @GetMapping("getLog/{id}")
    public Result getLog(@PathVariable String id){
        Log log = logService.getLog(id);
        return Result.ok().data("log",log);
    }


    @ApiOperation(value = "删除操作日志")
    @ControllerEndpoint(exceptionMessage = "删除操作日志失败", operation = "删除操作日志")
    @RequiresPermissions({"log:delete"})
    @DeleteMapping("deleteLog/{id}")
    public Result deleteLog(@PathVariable String id) {
        int i = logService.deleteLog(id);
        return Result.ok().data("delete",i);
    }

    @ApiOperation(value = "清空操作日志")
    @ControllerEndpoint(exceptionMessage = "清空操作日志失败", operation = "清空操作日志")
    @RequiresPermissions({"log:clear"})
    @DeleteMapping("clearLog")
    public Result clearLog() {
        int clear = logService.clearLog();
        return Result.ok().data("clear",clear);
    }


}
